﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using RenderHeads.Media.AVProVideo;
using DG.Tweening;

[RequireComponent(typeof(Button))]
[AddComponentMenu(menuName: "Video Controller/VideoComponentExtend")]
public class VideoComponentExtend : MonoBehaviour
{
    public MediaPlayer MediaPlayerComponent;
    public GameObject MediaPlayerUI;
    public string VideoLink;
    [HideInInspector]
    public static RectTransform[] rangePos = new RectTransform[2];
    private bool canPlayAnim = true;
    private void Start()
    {
        SetUpVideoDoor();
        rangePos[0] = transform.parent.parent.parent.GetChild(1) as RectTransform;
        rangePos[1] = transform.parent.parent.parent.GetChild(2) as RectTransform;
    }

    private void SetUpVideoDoor() => GetComponent<Button>().onClick.AddListener(VideoDoorButtonPressed);


    private void VideoDoorButtonPressed()
    {
        AppUIManager.instance.currentPanel = AppUIManager.CurrentPanelType.PlayVideo;
        MediaPlayerComponent.OpenMedia(new MediaPath(VideoLink, MediaPathType.AbsolutePathOrURL), autoPlay:true);
        MediaPlayerUI.SetActive(true);
        
        Transform mediaPlayerUI = GameObject.FindGameObjectWithTag("MediaPlayerUI").GetComponent<Transform>();
        mediaPlayerUI.Find("Panel").GetComponent<RectTransform>().localPosition = Vector2.zero;
        Invoke("ChangePanelType", 0.25F);
    }

    private void ChangePanelType()
    {
        AppUIManager.instance.agoPanel = AppUIManager.CurrentPanelType.PlayVideo;
        AndroidJavaObject javaObject = new AndroidJavaObject("com.example.commercialpromotion.UnityToAndroid");
        javaObject.Call<bool>("showToast", "视频加载中，请稍等");
    }

    private void Update()
    {
        if (canPlayAnim)
        {
            //transform.position.y
            if ((transform.parent.GetChild(transform.parent.childCount - 2).position.y > rangePos[0].position.y || transform.parent.GetChild(0).position.y < rangePos[1].position.y))
            {
                canPlayAnim = false;
                foreach (Image item in transform.parent.GetComponentsInChildren<Image>())
                {
                    Tweener tweener = item.DOFade(0F, 0.2F);
                    tweener.OnComplete(() => { canPlayAnim = true; });
                }
                foreach (Text item in transform.parent.GetComponentsInChildren<Text>())
                {
                    Tweener tweener = item.DOFade(0F, 0.2F);
                    tweener.OnComplete(() => { canPlayAnim = true; });
                }
            }
            else
            {
                canPlayAnim = false;
                foreach (Image item in transform.parent.GetComponentsInChildren<Image>())
                {
                    Tweener tweener = item.DOFade(1F, 0.8F);
                    tweener.OnComplete(() => { canPlayAnim = true; });
                }
                foreach (Text item in transform.parent.GetComponentsInChildren<Text>())
                {
                    Tweener tweener = item.DOFade(1F, 0.8F);
                    tweener.OnComplete(() => { canPlayAnim = true; });
                }
            }
        }
        
    }
}
